  layui.config({
     base: CXL.ctxPath + '/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'form'], function(){
      var form = layui.form;
      var layer = layui.layer;
      // select 事件

      $(document).ready(function (){
          //初始化操作
          $.ajax({
              type: "post",
              url: CXL.ctxPath + '/sys/addTask/initMaterialInfo',
              success: function(res) {
                  if(res.success) {
                     var list = res.obj;
                     let materialNameContent = '';
                     let materialNoContent = '';
                      if(list != null){
                          $.each(list, function (index, item) {
                              materialNameContent += '<option value="'+item.materialNo+'">'+item.materialName+'</option>';
                              materialNoContent += '<option value="'+item.materialName+'" >'+item.materialNo+'</option>';

                          });
                          $('#materialNo').append(materialNameContent);
                          $('#materialName').append(materialNoContent);
                          //重新渲染 固定写法
                          layui.form.render("select");
                      }

                  }else {
                      CXL.warn(res.msg);
                  }
              }
          });
      });



      //监听提交
      form.on('submit(form-btn-save)', function(data){
          var param = data.field;

          var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引


          //提交 Ajax 成功后，关闭当前弹层并重载表格
          $.ajax({
              type: "post",
              url: CXL.ctxPath + '/sys/addTask/saveMaterialOpt',
              data: param,
              success: function(res) {
                  if(res.success) {
                      CXL.success(res.msg);
                      parent.layer.close(index); //再执行关闭

                  }else {
                      CXL.warn(res.msg);
                  }
              }
          });

      });


      //选择框联动
      form.on('select(select-materialName)', function(data){
          var materialName = $('#materialNo option:selected').text();
          layui.form.val('layuiadmin-app-form-list',{ 'materialName' : materialName});
      });

      //选择框联动
      form.on('select(select-materialNo)', function(data){
          var materialNo = $('#materialName option:selected').text();
          //对属性的value赋值
          layui.form.val('layuiadmin-app-form-list',{ 'materialNo' : materialNo});
      });


  })